package com.liufei1988.design.singleton;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 现成间的单例
 */
public class IdGeneratorC {
    private AtomicLong id=new AtomicLong(0);
    private static final ConcurrentHashMap<Long, IdGeneratorC> instance=new ConcurrentHashMap<Long, IdGeneratorC>();
    private IdGeneratorC(){};

    public static IdGeneratorC getInstance(){
        Long currentThreadId=Thread.currentThread().getId();
        instance.putIfAbsent(currentThreadId,new IdGeneratorC());
        return instance.get(currentThreadId);
    }
    public long getId(){
        return id.incrementAndGet();
    }
}
